using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class ClientServiceItem
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("ItemID", "HL_ClientServiceItem");
		}
		public bool Exists(int ItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_ClientServiceItem");
			stringBuilder.Append(" where ItemID=@ItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ItemID", SqlDbType.Int, 4)
			};
			array[0].Value = ItemID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.ClientServiceItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_ClientServiceItem(");
			stringBuilder.Append("ClientID,ContacterID,OrderID,ServiceTime,ServiceType,ServiceMode,ServiceTitle,ServiceContent,ServiceResult,TakeTime,ServicePoint,Processor,Inputer,Feedback,ConfirmTime,ConfirmCaller,ConfirmScore,ConfirmFeedback,Remark)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@ClientID,@ContacterID,@OrderID,@ServiceTime,@ServiceType,@ServiceMode,@ServiceTitle,@ServiceContent,@ServiceResult,@TakeTime,@ServicePoint,@Processor,@Inputer,@Feedback,@ConfirmTime,@ConfirmCaller,@ConfirmScore,@ConfirmFeedback,@Remark)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4),
				new SqlParameter("@ContacterID", SqlDbType.Int, 4),
				new SqlParameter("@OrderID", SqlDbType.Int, 4),
				new SqlParameter("@ServiceTime", SqlDbType.DateTime),
				new SqlParameter("@ServiceType", SqlDbType.NVarChar, 50),
				new SqlParameter("@ServiceMode", SqlDbType.NVarChar, 50),
				new SqlParameter("@ServiceTitle", SqlDbType.NVarChar, 50),
				new SqlParameter("@ServiceContent", SqlDbType.NText),
				new SqlParameter("@ServiceResult", SqlDbType.NVarChar, 50),
				new SqlParameter("@TakeTime", SqlDbType.NVarChar, 50),
				new SqlParameter("@ServicePoint", SqlDbType.Int, 4),
				new SqlParameter("@Processor", SqlDbType.NVarChar, 50),
				new SqlParameter("@Inputer", SqlDbType.NVarChar, 50),
				new SqlParameter("@Feedback", SqlDbType.NText),
				new SqlParameter("@ConfirmTime", SqlDbType.DateTime),
				new SqlParameter("@ConfirmCaller", SqlDbType.NVarChar, 50),
				new SqlParameter("@ConfirmScore", SqlDbType.Int, 4),
				new SqlParameter("@ConfirmFeedback", SqlDbType.NText),
				new SqlParameter("@Remark", SqlDbType.NText)
			};
			array[0].Value = model.ClientID;
			array[1].Value = model.ContacterID;
			array[2].Value = model.OrderID;
			array[3].Value = model.ServiceTime;
			array[4].Value = model.ServiceType;
			array[5].Value = model.ServiceMode;
			array[6].Value = model.ServiceTitle;
			array[7].Value = model.ServiceContent;
			array[8].Value = model.ServiceResult;
			array[9].Value = model.TakeTime;
			array[10].Value = model.ServicePoint;
			array[11].Value = model.Processor;
			array[12].Value = model.Inputer;
			array[13].Value = model.Feedback;
			array[14].Value = model.ConfirmTime;
			array[15].Value = model.ConfirmCaller;
			array[16].Value = model.ConfirmScore;
			array[17].Value = model.ConfirmFeedback;
			array[18].Value = model.Remark;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.ClientServiceItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_ClientServiceItem set ");
			stringBuilder.Append("ClientID=@ClientID");
			if (!string.IsNullOrEmpty(model.ContacterID.ToString()))
			{
				stringBuilder.Append(",ContacterID=" + model.ContacterID);
			}
			if (!string.IsNullOrEmpty(model.OrderID.ToString()))
			{
				stringBuilder.Append(",OrderID=" + model.OrderID);
			}
			if (model.ServiceTime.HasValue)
			{
				stringBuilder.Append(",ServiceTime='" + model.ServiceTime + "'");
			}
			if (!string.IsNullOrEmpty(model.ServiceType))
			{
				stringBuilder.Append(",ServiceType='" + model.ServiceType + "'");
			}
			if (!string.IsNullOrEmpty(model.ServiceMode))
			{
				stringBuilder.Append(",ServiceMode='" + model.ServiceMode + "'");
			}
			if (!string.IsNullOrEmpty(model.ServiceTitle))
			{
				stringBuilder.Append(",ServiceTitle='" + model.ServiceTitle + "'");
			}
			if (!string.IsNullOrEmpty(model.ServiceContent))
			{
				stringBuilder.Append(",ServiceContent='" + model.ServiceContent + "'");
			}
			if (!string.IsNullOrEmpty(model.ServiceResult))
			{
				stringBuilder.Append(",ServiceResult='" + model.ServiceResult + "'");
			}
			if (!string.IsNullOrEmpty(model.TakeTime))
			{
				stringBuilder.Append(",TakeTime='" + model.TakeTime + "'");
			}
			if (!string.IsNullOrEmpty(model.ServicePoint.ToString()))
			{
				stringBuilder.Append(",ServicePoint=" + model.ServicePoint);
			}
			if (!string.IsNullOrEmpty(model.Processor))
			{
				stringBuilder.Append(",Processor='" + model.Processor + "'");
			}
			if (!string.IsNullOrEmpty(model.Inputer))
			{
				stringBuilder.Append(",Inputer='" + model.Inputer + "'");
			}
			if (!string.IsNullOrEmpty(model.Feedback))
			{
				stringBuilder.Append(",Feedback='" + model.Feedback + "'");
			}
			if (model.ConfirmTime.HasValue)
			{
				stringBuilder.Append(",ConfirmTime='" + model.ConfirmTime + "'");
			}
			if (!string.IsNullOrEmpty(model.ConfirmCaller))
			{
				stringBuilder.Append(",ConfirmCaller='" + model.ConfirmCaller + "'");
			}
			if (!string.IsNullOrEmpty(model.ConfirmScore.ToString()))
			{
				stringBuilder.Append(",ConfirmScore=" + model.ConfirmScore);
			}
			if (!string.IsNullOrEmpty(model.ConfirmFeedback))
			{
				stringBuilder.Append(",ConfirmFeedback='" + model.ConfirmFeedback + "'");
			}
			if (!string.IsNullOrEmpty(model.Remark))
			{
				stringBuilder.Append(",Remark='" + model.Remark + "'");
			}
			stringBuilder.Append(" where ItemID=@ItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ClientID", SqlDbType.Int, 4),
				new SqlParameter("@ItemID", SqlDbType.Int, 4)
			};
			array[0].Value = model.ClientID;
			array[1].Value = model.ItemID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int ItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientServiceItem ");
			stringBuilder.Append(" where ItemID=@ItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ItemID", SqlDbType.Int, 4)
			};
			array[0].Value = ItemID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string ItemIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientServiceItem ");
			stringBuilder.Append(" where ItemID in (" + ItemIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.ClientServiceItem GetModel(int ItemID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 ItemID,ClientID,ContacterID,OrderID,ServiceTime,ServiceType,ServiceMode,ServiceTitle,ServiceContent,ServiceResult,TakeTime,ServicePoint,Processor,Inputer,Feedback,ConfirmTime,ConfirmCaller,ConfirmScore,ConfirmFeedback,Remark from HL_ClientServiceItem ");
			stringBuilder.Append(" where ItemID=@ItemID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ItemID", SqlDbType.Int, 4)
			};
			array[0].Value = ItemID;
			HKB.Data.Model.ClientServiceItem clientServiceItem = new HKB.Data.Model.ClientServiceItem();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.ClientServiceItem result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.ClientServiceItem DataRowToModel(DataRow row)
		{
			HKB.Data.Model.ClientServiceItem clientServiceItem = new HKB.Data.Model.ClientServiceItem();
			if (row != null)
			{
				if (row["ItemID"] != null && row["ItemID"].ToString() != "")
				{
					clientServiceItem.ItemID = int.Parse(row["ItemID"].ToString());
				}
				if (row["ClientID"] != null && row["ClientID"].ToString() != "")
				{
					clientServiceItem.ClientID = new int?(int.Parse(row["ClientID"].ToString()));
				}
				if (row["ContacterID"] != null && row["ContacterID"].ToString() != "")
				{
					clientServiceItem.ContacterID = new int?(int.Parse(row["ContacterID"].ToString()));
				}
				if (row["OrderID"] != null && row["OrderID"].ToString() != "")
				{
					clientServiceItem.OrderID = new int?(int.Parse(row["OrderID"].ToString()));
				}
				if (row["ServiceTime"] != null && row["ServiceTime"].ToString() != "")
				{
					clientServiceItem.ServiceTime = new System.DateTime?(System.DateTime.Parse(row["ServiceTime"].ToString()));
				}
				if (row["ServiceType"] != null)
				{
					clientServiceItem.ServiceType = row["ServiceType"].ToString();
				}
				if (row["ServiceMode"] != null)
				{
					clientServiceItem.ServiceMode = row["ServiceMode"].ToString();
				}
				if (row["ServiceTitle"] != null)
				{
					clientServiceItem.ServiceTitle = row["ServiceTitle"].ToString();
				}
				if (row["ServiceContent"] != null)
				{
					clientServiceItem.ServiceContent = row["ServiceContent"].ToString();
				}
				if (row["ServiceResult"] != null && row["ServiceResult"].ToString() != "")
				{
					clientServiceItem.ServiceResult = row["ServiceResult"].ToString();
				}
				if (row["TakeTime"] != null && row["TakeTime"].ToString() != "")
				{
					clientServiceItem.TakeTime = row["TakeTime"].ToString();
				}
				if (row["ServicePoint"] != null && row["ServicePoint"].ToString() != "")
				{
					clientServiceItem.ServicePoint = int.Parse(row["ServicePoint"].ToString());
				}
				if (row["Processor"] != null)
				{
					clientServiceItem.Processor = row["Processor"].ToString();
				}
				if (row["Inputer"] != null)
				{
					clientServiceItem.Inputer = row["Inputer"].ToString();
				}
				if (row["Feedback"] != null)
				{
					clientServiceItem.Feedback = row["Feedback"].ToString();
				}
				if (row["ConfirmTime"] != null && row["ConfirmTime"].ToString() != "")
				{
					clientServiceItem.ConfirmTime = new System.DateTime?(System.DateTime.Parse(row["ConfirmTime"].ToString()));
				}
				if (row["ConfirmCaller"] != null)
				{
					clientServiceItem.ConfirmCaller = row["ConfirmCaller"].ToString();
				}
				if (row["ConfirmScore"] != null && row["ConfirmScore"].ToString() != "")
				{
					clientServiceItem.ConfirmScore = new int?(int.Parse(row["ConfirmScore"].ToString()));
				}
				if (row["ConfirmFeedback"] != null)
				{
					clientServiceItem.ConfirmFeedback = row["ConfirmFeedback"].ToString();
				}
				if (row["Remark"] != null)
				{
					clientServiceItem.Remark = row["Remark"].ToString();
				}
			}
			return clientServiceItem;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ItemID,ClientID,ContacterID,OrderID,ServiceTime,ServiceType,ServiceMode,ServiceTitle,ServiceContent,ServiceResult,TakeTime,ServicePoint,Processor,Inputer,Feedback,ConfirmTime,ConfirmCaller,ConfirmScore,ConfirmFeedback,Remark ");
			stringBuilder.Append(" FROM HL_ClientServiceItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" ItemID,ClientID,ContacterID,OrderID,ServiceTime,ServiceType,ServiceMode,ServiceTitle,ServiceContent,ServiceResult,TakeTime,ServicePoint,Processor,Inputer,Feedback,ConfirmTime,ConfirmCaller,ConfirmScore,ConfirmFeedback,Remark ");
			stringBuilder.Append(" FROM HL_ClientServiceItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_ClientServiceItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.ItemID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_ClientServiceItem T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
